package game.gfx;

import game.util.Logger;

public abstract class AbstractRunnable
{
    private enum Status
    {
        RUNNING, PAUSE, RESET
    }
    
    private volatile String info   = "";
    private volatile Status status = Status.RESET;
    
    public void setInfo(String info)
    {
        this.info = info;
    }
    
    public String getInfo()
    {
        return info;
    }
    
    public boolean isRunning()
    {
        return status == Status.RUNNING;
    }
    
    public boolean isReset()
    {
        return status == Status.RESET;
    }
    
    public boolean isPaused()
    {
        return status == Status.PAUSE;
    }
    
    protected Status getStatus()
    {
        return status;
    }
    
    private void setStatus(Status status)
    {
        Logger.debug("STATUS: ", status);
        this.status = status;
    }
    
    protected final void start()
    {
        onStart();
        setStatus(Status.RUNNING);
    }
    
    protected void onStart()
    {
    }
    
    public final void pause()
    {
        if (isPaused() == false)
        {
            onPause();
            setStatus(Status.PAUSE);
        }
    }
    
    protected void onPause()
    {
    }
    
    protected final void resume()
    {
        if (isPaused())
        {
            onResume();
            setStatus(Status.RUNNING);
        }
    }
    
    protected void onResume()
    {
    }
    
    protected final void reset()
    {
        onReset();
        setStatus(Status.RESET);
    }
    
    protected void onReset()
    {
    }
    
    protected final void restart()
    {
        reset();
        start();
    }
    
    protected abstract void doNext();
    
    public void update()
    {
        if (isRunning() && isPaused() == false)
        {
            doNext();
        }
    }
}
